package com.hungtrung.script;

import java.util.ArrayList;
import java.util.Random;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.input.GestureDetector.GestureListener;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.hungtrung.entity.Ga;
import com.hungtrung.entity.GameOverScreen;
import com.hungtrung.entity.GiaCam;
import com.hungtrung.entity.HelpScreen;
import com.hungtrung.entity.Ngang;
import com.hungtrung.entity.Ro;
import com.hungtrung.entity.ScoreScreen;
import com.hungtrung.entity.Shit;
import com.hungtrung.entity.Trung;
import com.hungtrung.entity.TrungChatThai;
import com.hungtrung.entity.Vit;
import com.hungtrung.game.GameConfig;

public class HungTrungScript { //kich ban game	
	private ArrayList<GiaCam> listGiaCam = new ArrayList<GiaCam>();	
	private Ro  ro = new Ro(new Vector2(GameConfig.RO_X_SPAW, GameConfig.RO_Y_SPAW), new Vector2(0, 0));
	private int countFault = 0; //hung nham chat thai hoac k hung kip
	private int countScore = 0; // hung  trung dc cong diem
	private int soGa, soVit, soNgang = 0;	//so ga, vit, ngang duoc tao ra
	private int level = 1;//moi lan hung 20 trung tang 1 level
	private boolean isGameOver = false;
	private boolean isGameStart = false;	
	private boolean is_H_KEY_PRESS_TOGGLE  = false;	//true = bat help
	private BitmapFont fontScore = new BitmapFont();
	private BitmapFont fontGameStart;
	private ScoreScreen scoreScreen = new ScoreScreen(new Vector2(0, 0), new Vector2(0, 0));
	private HelpScreen helpScreen = new HelpScreen(new Vector2(GameConfig.WIDTH/4, GameConfig.HEIGHT/2), new Vector2(0, 0));
	private GameOverScreen gameOverScreen =  new GameOverScreen(new Vector2(0, GameConfig.HEIGHT/2), new Vector2(0, 0));	

	/*private  SpriteBatch sb;
	public HungTrungScript(SpriteBatch sb){
		this.sb = sb;
	}*/
	private void taoRo(SpriteBatch sb){				
		ro.render(sb);
		ro.update();
	}
	private void themGiaCamDesktop(SpriteBatch sb) {		
		if(!isGameOver){			
			if (Gdx.input.isKeyJustPressed(Keys.G)) {// them 1 con ga				
				soGa++;
				GiaCam giaCam = new Ga(new Vector2(GameConfig.WIDTH - 82, GameConfig.HEIGHT - 82), new Vector2(0, 0));
				listGiaCam.add(giaCam);
				
			}
			if (Gdx.input.isKeyJustPressed(Keys.V)) {// them 1 con vit				
				soVit++;
				GiaCam giaCam = new Vit(new Vector2(GameConfig.WIDTH - 82, GameConfig.HEIGHT - 82), new Vector2(0, 0));
				listGiaCam.add(giaCam);
				
			}
	
			if (Gdx.input.isKeyJustPressed(Keys.N)) {// them 1 con ngang				
				soNgang++;
				GiaCam giaCam = new Ngang(new Vector2(GameConfig.WIDTH - 82, GameConfig.HEIGHT - 82), new Vector2(0,
						0));
				listGiaCam.add(giaCam);
				
			}							
		}
		/*if(giaCam!=null){
			giaCam.ticker(new Vector2(giaCam.getPosition().x, 600 - 82), sb);
		}*/
		if (!listGiaCam.isEmpty()) {		
			for (int i = 0; i < listGiaCam.size(); i++) { // cap nhat vi tri, render len man hinh cho tat ca cac con gia cam moi them
				GiaCam curGiaCam =  listGiaCam.get(i);
				Vector2 pos = curGiaCam.getPosition(); 
				curGiaCam.deTrungShit(new Vector2(pos.x, GameConfig.HEIGHT-82), sb);//add trung || shit cho tung con gia cam				
				curGiaCam.render(sb);				
				curGiaCam.update();						
			}
		
		}
	}	
	
	private void themGiaCamAndroid(SpriteBatch sb){
		
		if(!isGameOver){				
						
			if (Gdx.input.isKeyJustPressed(Keys.MENU)) {// them 1 con ga				
				
				GiaCam giaCam;
						
				Random random = new Random();
				int ranNum = random.nextInt(10) + 1;
				
				 if(ranNum%2 == 0) {
					soVit++;
					giaCam = new Vit(new Vector2(GameConfig.WIDTH - 82, GameConfig.HEIGHT - 82), new Vector2(0, 0));
				}else
					if(ranNum%3 == 0){
						soNgang++;
						giaCam = new Ngang(new Vector2(GameConfig.WIDTH - 82, GameConfig.HEIGHT - 82), new Vector2(0, 0));
					}else{
						soGa++;
						giaCam = new Ga(new Vector2(GameConfig.WIDTH - 82, GameConfig.HEIGHT - 82), new Vector2(0, 0));
					}
				listGiaCam.add(giaCam);
			}					
		}
		
		if (!listGiaCam.isEmpty()) {		
			for (int i = 0; i < listGiaCam.size(); i++) { // cap nhat vi tri, render len man hinh cho tat ca cac con gia cam moi them
				GiaCam curGiaCam =  listGiaCam.get(i);
				Vector2 pos = curGiaCam.getPosition(); 
				curGiaCam.render(sb);				
				curGiaCam.update();	
				curGiaCam.deTrungShit(new Vector2(pos.x, GameConfig.HEIGHT-82), sb);//add trung || shit cho tung con gia cam												
			}
		
		}
		}	
	
	
	private void tinhDiem(SpriteBatch sb) {
		if (!isGameOver) {
			if (!listGiaCam.isEmpty()) {
				for (int i = 0; i < listGiaCam.size(); i++) {
					// danh sach trung cua con gia cam x
					
					if (!isScore(i)) {
						countFault++;						
					} 
				}//end for
				if (countFault >= 3) {
					displayGameOver(sb);
					// reset game						
				}				
			}
		} else {//game over
			gameOverScreen.render(sb);
			resetGame();			
			//gameOver(sb);
		}
	}
	
	private boolean isScore(int giaCamIndex){
		/*
		 * true = ghi diem
		 * false = khong ghi diem
		 * */
		ArrayList<TrungChatThai> t = listGiaCam.get(giaCamIndex).getListTrungChatThai();//list trung, shit
		//System.out.println(t.size());
		for (int i = 0; i < t.size(); i++) {
			/*if (t.get(i).getPosition().y < 0 && (t.get(i) instanceof Shit)) {//shit di het height
				t.remove(i);								
			} */
			if (t.get(i).getPosition().y < 0 && (t.get(i) instanceof Trung)) {//trung di het height
				t.remove(i);
				return false;
			} 
				if (isCollision(t.get(i))) {
					if (t.get(i) instanceof Shit) {
						t.remove(i);
						return false;
					}
					if (t.get(i) instanceof Trung) {
						countScore++;						
						t.remove(i);		
						
						if(countScore == level*GameConfig.SCORE_LEVEL_UP){							
							level++;							
							TrungChatThai.def_level = level;
						}
						return true;
					}
				}			
		}
		return true;
	}
	
	private boolean isCollision(TrungChatThai t){
		//Rectangle intersection = new Rectangle();
		float t_y = t.getPosition().y;
		float t_x = t.getPosition().x;
		float r_y = ro.getPosition().y;
		float r_x = ro.getPosition().x;
		Rectangle r1 = new Rectangle(t_x, t_y, t.getWidth(), t.getHeight());
		Rectangle r2 = new Rectangle(r_x, r_y, ro.getWidth(), ro.getHeight());
			if(t_y>=r_y && t_y<=(r_y+ro.getHeight()/2)){
				if(Intersector.overlaps(r1, r2) && t_y <= r_y +(ro.getHeight()/2)){								
					return true;		
				}
			}		
		return false;
	}
	
	private void printScore(SpriteBatch sb){				
		fontScore.draw(sb, "X "+ String.valueOf(countScore) , 45, 30);
		fontScore.draw(sb, "X "+ String.valueOf(countFault) , 130, 30);
		fontScore.draw(sb, "X "+ String.valueOf(soGa) , 210, 30);
		fontScore.draw(sb, "X "+ String.valueOf(soVit) , 290, 30);
		fontScore.draw(sb, "X "+ String.valueOf(soNgang) , 370, 30);
	}
	
	public void gameStart(SpriteBatch sb){
		if(!isGameStart){			//game chua duoc bat dau
			fontGameStart = new BitmapFont();			
			switch (Gdx.app.getType()) {
				case Android:
					fontGameStart.draw(sb, "CHAM MAN HINH DE BAT DAU", GameConfig.WIDTH/2, GameConfig.HEIGHT/2);
					if(Gdx.input.justTouched()){
						themGiaCamAndroid(sb);
						isGameStart = true;	
					}
					break;
			
				case Desktop:					
					fontGameStart.draw(sb, "BAM PHIM SPACE DE BAT DAU CHOI", GameConfig.WIDTH/6, GameConfig.HEIGHT/2);
					fontGameStart.draw(sb, "BAM PHIM H XEM TRO GIUP", GameConfig.WIDTH/6, GameConfig.HEIGHT/2 - 50);
					if(Gdx.input.isKeyJustPressed(Keys.H)){								
						is_H_KEY_PRESS_TOGGLE = !is_H_KEY_PRESS_TOGGLE;						
					}
					if(is_H_KEY_PRESS_TOGGLE){
						helpScreen.render(sb);							
					}
					if(Gdx.input.isKeyJustPressed(Keys.SPACE)){//bam phim space de bat dau choi
						soGa++;
						GiaCam giaCam = new Ga(new Vector2(GameConfig.WIDTH - 82, GameConfig.HEIGHT - 82), new Vector2(0, 0));
						listGiaCam.add(giaCam);
						isGameStart = true;						
						if(is_H_KEY_PRESS_TOGGLE) helpScreen.dispose();
					}
					break;
				default:
					break;
			}			
		}else{	// game da bat dau		
			taoRo(sb);
			switch (Gdx.app.getType()) {
				case Android:
				themGiaCamAndroid(sb);
				break;
				
				case Desktop:
				themGiaCamDesktop(sb);
				break;
				default:
				break;
			}
		}
		
		scoreScreen.render(sb);		
		tinhDiem(sb);
		printScore(sb);
	}
			
	private void displayGameOver(SpriteBatch sb){//hien thi man hinh gameover
		isGameOver = true;
		isGameStart = false;
		listGiaCam.removeAll(listGiaCam);		
		//soGa = soVit = soNgang = 0;
		gameOverScreen.render(sb);
	}
		
	
	private void resetGame(){			
		if(Gdx.input.isKeyJustPressed(Keys.SPACE) || (Gdx.input.isTouched())){
			gameOverScreen.dispose();
			isGameOver = false;				
			soGa = soVit = soNgang = 0;
			countFault = countScore = 0;
					
		}
	}
	
	
}
